/*
 * @Author: your name
 * @Date: 2020-04-22 15:30:23
 * @LastEditTime: 2020-06-22 13:49:29
 * @LastEditors: kl2018
 * @Description: In User Settings Edit
 * @FilePath: \odoc\stores\odocTypeChangeStore.js
 */
import { observable, action } from 'mobx';
import { WeaForm, WeaTableNew } from 'comsMobx';
import { WeaTools, WeaLocaleProvider } from 'ecCom';
import { message, Modal } from 'antd';

const getLabel = WeaLocaleProvider.getLabel;
const { TableStore } = WeaTableNew;

export default class OdocTypeChangeStore {
  @observable changeForm = new WeaForm();
  @observable tableTitle = '';
  @observable tableStore = new TableStore();
  @observable loading = false;

  @observable workflowId = '';
  @observable requestId = '';
  @observable f_weaver_belongto_userid = '';
  @observable f_weaver_belongto_usertype = '';

  @action
  setProperty = (map) => {
    for (let i in map) {
      this[i] = map[i];
    }
  }

  @action
  getContent = () => {
    const params = {
      workflowId: this.workflowId,
      requestId: this.requestId,
    };
    WeaTools.callApi('/api/odoc/docTypeChange/getDocTypeChangeRevisionInfo', 'POST', params).then((data) => {
      this.changeForm = new WeaForm();
      this.tableStore = new TableStore();
      const condition = [{
        title: data.changeInfoItem.title,
        items: data.changeInfoItem.items,
        defaultshow: true,
      }];
      this.changeForm.setCondition(condition);
      this.tableTitle = data.docTypeChangDataItem.title1;
      if (data.sessionkey) {
        this.tableStore.getDatas(data.sessionkey, 1);
      }
    });
  }

  @action
  submitChanges = (onCancel) => {
    this.changeForm.validateForm().then((f) => {
      if (f.isValid) {
        const params = {
          workflowId: this.workflowId,
          requestId: this.requestId,
          f_weaver_belongto_userid: this.f_weaver_belongto_userid,
          f_weaver_belongto_usertype: this.f_weaver_belongto_usertype,
          ...this.changeForm.getFormParams(),
        };
        const loading = message.loading(getLabel('385366', '正在执行中...'), 0);
        this.loading = true;
        WeaTools.callApi('/api/odoc/docTypeChange/submitDocTypeChange', 'POST', params).then((res) => {
          loading();
          if (res.api_status) {
            this.loading = false;
            message.success(res.msg);
            onCancel();
          } else {
            Modal.warning({
              title: getLabel('131329', '信息确认'),
              content: res.msg,
            });
            if (res.msgError == 0) {
              onCancel();
            }
            this.loading = false;
          }
        });
      } else {
        f.showErrors();
      }
    });
  }
}
